<?php

namespace App\Http\Middleware;

use App\Models\User;
use Closure;

class TakeToken
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $token = $request->get('token');

        if($token=="" || $token==null){
            return redirect(url('api/user/not_found'));
        }
        if(!$user = User::where('token',$token)->first()){
            return redirect(url('api/user/not_found'));
        }
        $request->attributes->add(compact('user'));
        return $next($request);
    }
}
